//
//  ShowAnimationView.m
//  MovingLoaniOS
//
//  Created by 谷峰 on 2019/10/9.
//  Copyright © 2019 GWMFC. All rights reserved.
//

#import "ShowAnimationView.h"
#import "Macros.h"
@interface ShowAnimationView ()
 
@property (nonatomic, strong) UIView  *contentView;
@end

@implementation ShowAnimationView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self layoutAllSubviews];
       // self.contentDict = contentDict;
        
    }
    return self;
}
- (void)setContentDict:(NSDictionary*)contentDict{
    _contentDict = contentDict;
     [self layoutAllSubviews];
}
- (void)layoutAllSubviews{
    
    /*创建灰色背景*/
    UIView *bgView = [[UIView alloc] initWithFrame:self.frame];
    bgView.alpha = 0.3;
    bgView.backgroundColor = [UIColor blackColor];
    [self addSubview:bgView];
    
    /*添加手势事件,移除View*/
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView:)];
    [bgView addGestureRecognizer:tapGesture];
    
    /*创建显示View*/
    _contentView = [[UIView alloc] init];
    _contentView.frame = CGRectMake(0, self.frame.size.height-360, self.frame.size.width, 360);
    _contentView.backgroundColor= HEXCOLOR(0xF2F5FB);
    _contentView.layer.cornerRadius = 12;
    _contentView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
    _contentView.layer.masksToBounds = YES;
    [self addSubview:_contentView];
  /*可以继续在其中添加一些View 虾米的*/
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 110)];
    image.image = [UIImage imageNamed:@"rrkk"];
    [_contentView addSubview:image];
    
    /*创建显示View*/
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"意向信息";
    lab.textColor = UIColor.whiteColor;
    lab.frame = CGRectMake(0, 14, self.frame.size.width, 30);
    lab.textAlignment = NSTextAlignmentCenter;
    lab.font = [UIFont systemFontOfSize:20];
    [_contentView addSubview:lab];
    
    
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(14, 55, self.frame.size.width-30, 90);
    view.backgroundColor=[UIColor whiteColor];
    view.layer.cornerRadius = 4;
    view.layer.masksToBounds = YES;
    [_contentView addSubview:view];
    [self createNameLabAddView:view];
    [self createLabView:@[@"天猫终审额度:",@"意向车型:",@"金融产品:",@"贷款期限:",@"申请时间:"]];
    NSString *date = @"";
    if (self.contentDict[@"cdate"]) {
        date = [self.contentDict[@"cdate"] substringToIndex:16];
    }
    [self createContentLabView:@[[NSString stringWithFormat:@"%@元",self.contentDict[@"creditAmount"]],self.contentDict[@"carInfo"],self.contentDict[@"productInfo"],self.contentDict[@"term"],date]];
    
}

-(void)createNameLabAddView:(UIView*)subView{
    UILabel *lab = [[UILabel alloc] init];
    
    
    //lab.frame = CGRectMake(15, 15, 220, 28);
    lab.text = _contentDict[@"customerName"];
    lab.textColor = HEXCOLOR(0x666666);
    lab.textAlignment = NSTextAlignmentLeft;
//    [lab sizeToFit];
    lab.font = [UIFont systemFontOfSize:20];
    lab.numberOfLines = 0;//根据最大行数需求来设置
    lab.lineBreakMode = NSLineBreakByTruncatingTail;
    CGSize maximumLabelSize = CGSizeMake(10000, 28);//labelsize的最大值
    //关键语句
    CGSize expectSize = [lab sizeThatFits:maximumLabelSize];
   //别忘了把frame给回label，如果用xib加了约束的话可以只改一个约束的值
    lab.frame = CGRectMake(15, 15, expectSize.width, expectSize.height);
    [subView addSubview:lab];
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"联系电话" forState:UIControlStateNormal];
    [button setTitleColor:HEXCOLOR(0x4166C9) forState:UIControlStateNormal];
    button.layer.borderWidth = 1;
    button.layer.borderColor = HEXCOLOR(0x4166C9).CGColor;
    button.layer.cornerRadius = 8;
    
    if (expectSize.width > (subView.frame.size.width-100)) {
        button.frame  = CGRectMake(CGRectGetMaxX(subView.frame)-130, CGRectGetMaxY(lab.frame)+10, 100, 30);
    }else{
        button.frame  = CGRectMake(CGRectGetMaxX(subView.frame)-130, 15, 100, 30);
    }
    
    [button setImage:[UIImage imageNamed:@"phoneImage"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(callPhoneAction) forControlEvents:UIControlEventTouchUpInside];
    [subView addSubview:button];
    
    UILabel *phoneLab = [[UILabel alloc] init];
    phoneLab.text = _contentDict[@"mobile"];
    phoneLab.textColor = HEXCOLOR(0x4760A4);
    phoneLab.frame = CGRectMake(CGRectGetMinX(button.frame)-132, 15, 128, 28);
    phoneLab.font = [UIFont systemFontOfSize:18];
    if (expectSize.width > subView.frame.size.width-100) {
        phoneLab.hidden = YES;
    }
    [subView addSubview:phoneLab];
    
    
    
    UIImageView *idImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(lab.frame)+12, 22, 22)];
    idImage.image = [UIImage imageNamed:@"idImage"];
    [subView addSubview:idImage];
    
    UILabel *idLab = [[UILabel alloc] init];
    idLab.text = _contentDict[@"certNo"];
    idLab.textColor = HEXCOLOR(0x666666);
    idLab.frame = CGRectMake(CGRectGetMaxX(idImage.frame)+12, CGRectGetMaxY(lab.frame)+10, 165, 28);
    idLab.textAlignment = NSTextAlignmentLeft;
    idLab.font = [UIFont systemFontOfSize:15];
    [subView addSubview:idLab];
}
- (void)createLabView:(NSArray*)titleArray{
    for (int i = 0; i < titleArray.count; i++) {
        UILabel *lab = [[UILabel alloc] init];
           lab.text = titleArray[i];
           lab.textColor = HEXCOLOR(0x666666);
           lab.frame = CGRectMake(18, 120 + (i+1)*35, 100, 40);
           lab.textAlignment = NSTextAlignmentLeft;
           lab.font = [UIFont systemFontOfSize:15];
           [_contentView addSubview:lab];
    }
}
- (void)createContentLabView:(NSArray*)contentArray{
    for (int i = 0; i < contentArray.count; i++) {
        UILabel *lab = [[UILabel alloc] init];
           lab.text = [NSString stringWithFormat:@"%@",contentArray[i]];
           lab.textColor = HEXCOLOR(0x666666);
           lab.frame = CGRectMake(35+100, 120 + (i+1)*35, Main_Screen_Width-150, 40);
           lab.textAlignment = NSTextAlignmentLeft;
           lab.font = [UIFont systemFontOfSize:15];
           lab.numberOfLines = 0;
           [_contentView addSubview:lab];
    }
}

-(void)callPhoneAction{
    
     NSMutableString* str=[[NSMutableString alloc]initWithFormat:@"telprompt://%@",_contentDict[@"mobile"]];
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:str] options:@{}  completionHandler: nil];
    
}
#pragma mark - 手势点击事件,移除View
- (void)dismissContactView:(UITapGestureRecognizer *)tapGesture{
    
    [self dismissContactView];
}
 
-(void)dismissContactView
{
    __weak typeof(self)weakSelf = self;
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
    
}
 
// 这里加载在了window上
-(void)showView
{
    UIWindow * window = [UIApplication sharedApplication].windows[0];
    [window addSubview:self];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
